package app.displayTree.special
{
    import app.displayTree.analyze.DisplayTreeAnalyzeSupport;
    import app.displayTree.special.sub.NodeHolderSpecialSymbol;
    import app.displayTree.special.sub.PlaceHolderSpecialSymbol;
    import app.displayTree.special.sub.QuadColourSpecialSymbol;
    import app.displayTree.special.sub.RectangleSpecialSymbol;
    import app.displayTree.special.sub.S3ScaleSpecialSymbolHandle;
    import app.displayTree.special.sub.S9ScaleSpecialSymbolHandle;
    import app.displayTree.special.sub.SpecialSymbolHandleBasic;
    import app.displayTree.special.sub.TileRepeatSpecialSymbol;

    import copyengine.infoTree.meta.CESDisplayObjectMeta;

    import flash.display.DisplayObject;
    import flash.display.Sprite;

    public final class SpecialSymbolManger
    {

        private var mAllSubSpecialSymbolVector:Vector.<SpecialSymbolHandleBasic>;

        public function SpecialSymbolManger()
        {
        }

        /**
         * 每一个SWF文件有两种分析方式,一种是ShareTexture另外一种是不Share
         * 无论是哪种方式,SpecialSymbol都仅初始化一次
         * (两种方式的区别在于分析每个导出类途中是否清空已分析的数据)
         */
        public function initialize():void
        {
            mAllSubSpecialSymbolVector = new Vector.<SpecialSymbolHandleBasic>();
        }

        public function tryToGenerateSpecialSymbolMeta(_symbol:Sprite, _support:DisplayTreeAnalyzeSupport):CESDisplayObjectMeta
        {
            var length:int = mAllSubSpecialSymbolVector.length;
            for (var i:int = 0; i < length; i++) {
                var symbolHandle:SpecialSymbolHandleBasic = mAllSubSpecialSymbolVector[i];
                var resultMeta:CESDisplayObjectMeta = symbolHandle.tryToGenerateSpecialSymbolMeta(_symbol, _support);
                if (resultMeta != null) {
                    return resultMeta;
                }
            }
            return null;
        }

        public function tryToPushSpecialSymbol(_symbolKey:String, _symbol:DisplayObject):Boolean
        {
            var detectionVector:Vector.<SpecialSymbolHandleBasic> = getDetectionVector();

            var length:int = detectionVector.length;
            for (var i:int = 0; i < length; i++) {
                var symbolHandle:SpecialSymbolHandleBasic = detectionVector[i];
                if (symbolHandle.tryToPushSpecialSymbol(_symbolKey, _symbol)) {
                    //当前SymbolHandle可以处理,记录到Vector中去,用于以后检测
                    //每个特殊Symbol都有一个自己的处理类,而不是公用
                    mAllSubSpecialSymbolVector.push(symbolHandle);
                    return true;
                }
            }
            return false;
        }

        private function getDetectionVector():Vector.<SpecialSymbolHandleBasic>
        {
            var detectionVector:Vector.<SpecialSymbolHandleBasic> = new Vector.<SpecialSymbolHandleBasic>();
            detectionVector.push(new RectangleSpecialSymbol()); //Mask
            detectionVector.push(new TileRepeatSpecialSymbol()); //Tile重复复制
            detectionVector.push(new PlaceHolderSpecialSymbol()); //PH
            detectionVector.push(new NodeHolderSpecialSymbol());//Node节点
            detectionVector.push(new QuadColourSpecialSymbol()); //Quad颜色块
            detectionVector.push(new S3ScaleSpecialSymbolHandle());//Scale3
            detectionVector.push(new S9ScaleSpecialSymbolHandle());//Scale9
            return detectionVector;
        }


    }
}
